//
//  ViewController.m
//  base64的加密与解密
//
//  Created by 伍银荣 on 2021/9/8.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    NSString *str = [self base64Encode:@"wuyin"];
    NSLog(@"str = %@",str);
    
    NSString *str2 = [self base64Decode:@"d3V5aW4="];
    NSLog(@"str2 = %@",str2);
}

//base64"加密"密码,无论是编码解码还是加密解密,都是直接操作的二进制数据
- (NSString *)base64Encode:(NSString *)str {
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}

//base64"解密"密码
- (NSString *)base64Decode:(NSString *)str {
    //base64解密
    //获取data类型的二进制数据
    NSData *data = [[NSData alloc]initWithBase64EncodedString:str options:0];
    //将data类型的二进制数据转化成字符串
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}

@end
